package v1

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"maya-service/bll"
	"maya-service/config"
	"maya-service/model"
	mallPo "maya-service/model/po/mall"
	"maya-service/utils"
	"maya-service/utils/request"
)

var GoodsCollector = &goodsCollector{}

func init() {
	RegisterRouter(GoodsCollector)
}

type goodsCollector struct{}

// Init
func (a *goodsCollector) Init(r *gin.RouterGroup) {
	g := r.Group("/goodsCollector")
	{
		g.POST("/create", a.create)
		g.POST("/update", a.update)
		g.POST("/list", a.list)
		g.POST("/delete", a.delete)
		g.POST("/detail", a.find)
	}
}

// create
func (a *goodsCollector) create(c *gin.Context) {
	var (
		in  = &model.GoodsCollectorCreateRequest{}
		err error
		id  int64
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if id, err = bll.GoodsCollector.Create(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}

	// 创建合集
	out, err := request.CreateOrUpdateGoods(c.GetHeader("Authorization"), config.Conf.Env, mallPo.CreateGoodsRequest{
		ThirdGoodsId: id,
		GoodsName:    in.GoodsName,
		GoodsDetails: in.GoodsDetail,
		GoodsPrice:   fmt.Sprintf("%v", in.GoodsPrice),
		GoodsStock:   in.GoodsStock,
		GoodsState:   in.GoodsState,
		ImageUrlList: in.ImageURLList,
	})
	if err != nil || out.GoodsId == 0 {
		fmt.Println(bll.GoodsCollector.Delete(c.Request.Context(), &model.GoodsCollectorDeleteRequest{Id: id}))
		c.Error(fmt.Errorf("创建合集失败"))
		return
	}

	//
	_ = bll.GoodsCollector.Update(c.Request.Context(), &model.GoodsCollectorUpdateRequest{
		Id:           id,
		ThirdGoodsId: &out.GoodsId,
	})

	utils.ResponseOk(c, struct {
		Id int64 `json:"id"`
	}{
		Id: id,
	})
}

// update
func (a *goodsCollector) update(c *gin.Context) {
	var (
		in  = &model.GoodsCollectorUpdateRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.GoodsCollector.Update(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}

// list
func (a *goodsCollector) list(c *gin.Context) {
	var (
		in  = &model.GoodsCollectorListRequest{}
		out = &model.GoodsCollectorListResponse{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.GoodsCollector.List(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}

	utils.ResponseOk(c, out)
}

// list
func (a *goodsCollector) find(c *gin.Context) {
	var (
		in  = &model.GoodsCollectorInfoRequest{}
		out = &model.GoodsCollectorInfo{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if out, err = bll.GoodsCollector.Find(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, out)
}

// delete
func (a *goodsCollector) delete(c *gin.Context) {
	var (
		in  = &model.GoodsCollectorDeleteRequest{}
		err error
	)

	if err = c.ShouldBindJSON(in); err != nil {
		c.Error(err)
		return
	}

	if err = bll.GoodsCollector.Delete(c.Request.Context(), in); err != nil {
		c.Error(err)
		return
	}
	utils.ResponseOk(c, nil)
}
